home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / System Utility / Reset Desktop / Source / resetdtdbs.c next >
C/C++ Source or Header  |  1993-02-14  |  4KB  |  168 lines

  1. /*
  2.    resetdtdbs.c
  3.    resets all desktop databases
  4.    by Brian Gaeke, dec 28 1992
  5. */
  6.  
  7. #define done(vcb) ((vcb) == (vcbQHdr->qTail)) && ((vcbQHdr->qHead) != (vcbQHdr->qTail))
  8. #include <Processes.h>
  9. #include <AppleEvents.h>
  10. #include <ShutDown.h>
  11.  
  12. short OpenDatabase(short vRefNum);
  13. void DeleteDatabase(short dtRefNum);
  14. void PrepareDatabase(short dtRefNum);
  15. void InitMac(void);
  16. void DoDialogs(void);
  17. void KillFinder(void);
  18. void Panic(OSErr err, unsigned char *routine);
  19.  
  20. pascal void main(void)
  21. {
  22.     QHdrPtr        vcbQHdr = GetVCBQHdr();
  23.     VCB            *aVCB = (VCB *) vcbQHdr->qHead;
  24.     short        itsVRefNum,
  25.                 itsDTRefNum;
  26.     
  27.     InitMac();
  28.     DoDialogs();
  29.     KillFinder();
  30.     
  31.     while (!done(aVCB))
  32.     {
  33.         itsVRefNum = aVCB->vcbVRefNum;
  34.         itsDTRefNum = OpenDatabase(itsVRefNum);
  35.         PrepareDatabase(itsDTRefNum);
  36.         DeleteDatabase(itsVRefNum);
  37.         aVCB = (VCB *) aVCB->qLink;
  38.     };
  39.     
  40.     (void)Alert(131,NULL);
  41.     ShutDwnStart();
  42. }
  43.  
  44. short OpenDatabase(short vRefNum)
  45. {
  46.     DTPBRec     dtpb;
  47.     short        dtRefNum;
  48.     OSErr        err;
  49.     
  50.     dtpb.ioCompletion = NULL;
  51.     dtpb.ioNamePtr = NULL;
  52.     dtpb.ioVRefNum = vRefNum;
  53.     err = PBDTGetPath(&dtpb);
  54.     if (err) Panic(err,"\pOpenDatabase");
  55.     return dtpb.ioDTRefNum;
  56. }
  57.  
  58. void DeleteDatabase(short vRefNum)
  59. {
  60.     DTPBRec        dtpb;
  61.     OSErr        err;
  62.     
  63.     dtpb.ioCompletion = NULL;
  64.     dtpb.ioVRefNum = vRefNum;
  65.     dtpb.ioIndex = 0;
  66.      err = PBDTDelete(&dtpb,false);
  67.     if (err) Panic(err,"\pDeleteDatabase");
  68. }
  69.  
  70. void PrepareDatabase(short dtRefNum)
  71. {
  72.     DTPBRec        dtpb;
  73.     OSErr        err;
  74.     
  75.     dtpb.ioCompletion = NULL;
  76.     dtpb.ioDTRefNum = dtRefNum;
  77.     err = PBDTFlush(&dtpb,false);
  78.     if (err) Panic(err,"\pPrepareDatabase--Flush");
  79.     
  80.     err = PBDTCloseDown(&dtpb);
  81.     if (err) Panic(err,"\pPrepareDatabase--CloseDown");
  82. }
  83.  
  84. void InitMac(void)
  85. {
  86.     short        x;
  87.     
  88.     InitGraf(&qd.thePort);
  89.     InitFonts();
  90.     FlushEvents(everyEvent,0);
  91.     InitWindows();
  92.     InitMenus();
  93.     TEInit();
  94.     InitDialogs(NULL);
  95.     InitCursor();
  96.     MaxApplZone();
  97.     for(x=1;x<11;x++)
  98.         MoreMasters();
  99. }
  100.  
  101. void DoDialogs(void)
  102. {
  103.     (void)Alert(128,NULL);
  104.     if (Alert(129,NULL) != ok) ExitToShell();
  105. }
  106.  
  107. void KillFinder(void)
  108. {
  109.     ProcessSerialNumber        psn,
  110.                             finder;
  111.     ProcessInfoRec            pir;
  112.     OSErr                    err;
  113.     AppleEvent                theEvent;
  114.     AEDesc                    theAddress;
  115.     Boolean                    gotEvent;
  116.     EventRecord                event;
  117.     short                    x;
  118.     
  119.     /* We have to do this because IM VI says not to call PBDTReset/PBDTDelete
  120.        "unless you are manipulating the desktop database in the absence
  121.        of the Finder"... and we all know that IM is the word of God... :)
  122.        So here goes... */
  123.        
  124.     psn.highLongOfPSN = 0;
  125.     psn.lowLongOfPSN = kNoProcess;
  126.     
  127.     pir.processInfoLength = sizeof(ProcessInfoRec);
  128.     pir.processAppSpec = NULL;
  129.     pir.processName = NULL;
  130.     
  131.     while (GetNextProcess(&psn) != procNotFound)
  132.     {
  133.         (void)GetProcessInformation(&psn,&pir);
  134.         if (pir.processSignature == 'MACS')
  135.         {
  136.             finder = psn;
  137.             break;
  138.         }
  139.     }
  140.     
  141.     /*** kill the app code swiped from C.K. Haun with minimal modifications ***/
  142.     (void)AECreateDesc(typeProcessSerialNumber, (Ptr)&finder,
  143.       sizeof(finder), &theAddress);
  144.     (void)AECreateAppleEvent(kCoreEventClass, kAEQuitApplication,
  145.       &theAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  146.     AEDisposeDesc(&theAddress);
  147.     AESend(&theEvent, NULL, kAENoReply + kAEAlwaysInteract +
  148.       kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  149.     AEDisposeDesc(&theEvent);
  150.     /*** end swiped code ***/
  151.     
  152.     /* give it time to work */
  153.     for(x=1; x<=5; x++)
  154.         gotEvent = EventAvail(everyEvent, &event);
  155.         
  156. }
  157.  
  158. void Panic(OSErr err, unsigned char *routine)
  159. {
  160.     Str255 errstr;
  161.     
  162.     NumToString((long) err,errstr);
  163.     ParamText(errstr,routine,"\p","\p");
  164.     (void)Alert(130,NULL);
  165.     ShutDwnStart();
  166. }
  167.  
  168.